package sample.crawler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import sample.crawler.base.BaseTask;
import sample.enums.MapType;
import sample.model.Company;
import sample.utils.IndexUtils;
import sample.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SogouTask extends BaseTask {



    protected static final Integer pageSize = 100;
    public SogouTask()
    {
        super.mapCode = MapType.sogou.getCode();
        super.pageSize = pageSize;
    }

    public SogouTask(String url)
    {
        super.mapCode = MapType.sogou.getCode();
        super.pageSize = pageSize;

        this.url = url;
    }


    @Override
    public String getUrl(Integer pageNo) {
        /**String url = null;
        try {
            String what = URLEncoder.encode("keyword:"+keywords, "utf-8");
            String range = URLEncoder.encode("city:"+area, "UTF-8");
            String pageinfo = URLEncoder.encode(pageNo + ","+pageSize, "UTF-8");
            url = "http://api.go2map.com/engine/api/search/json?what="+what+"&range="+range+"&pageinfo="+pageinfo;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }**/

        //String sogou = "http://api.go2map.com/engine/api/search/json?what={what}&range={range}&pageinfo={pageinfo}";
        String replace_url = null;
        try {
            replace_url =  url.replace("{what}", URLEncoder.encode("keyword:"+keywords, "utf-8")).replace("{range}",URLEncoder.encode("city:"+area, "utf-8")).replace("{pageinfo}", URLEncoder.encode(pageNo+","+pageSize, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return replace_url;
    }

    @Override
    public Integer getTotalCount(JSONObject res) {
        JSONObject response = res.getJSONObject("response");
        if(response == null) {
            return 0;
        }
        JSONObject data = response.getJSONObject("data");
        Integer total = data == null ? 0 : data.getInteger("resultcount");
        return total;
    }

    @Override
    public void fillTable(JSONObject jsonObject) throws Exception {
        JSONObject res = jsonObject.getJSONObject("response");
        if("error".equals(jsonObject.getString("status")) ) {
            throw new Exception(res.getString("error"));
        }

        if(res == null) {
            throw new Exception("no records");
        }
        JSONArray results = res.getJSONObject("data") == null ? null : res.getJSONObject("data").getJSONArray("feature");
        if(results == null)
        {
            throw new Exception("no records");
        }
        for (Object o : results) {
            JSONObject target = (JSONObject) o;

            // 过滤排除关键字
            String name = target.getString("caption");
            if(StringUtils.contains(name, exclusiveWords)) {
                continue;
            }

            JSONObject detail = target.getJSONObject("detail");
            // 过滤空电话
            String tel = detail.getString("phone");
            if(filterTel && StringUtils.isEmpty(tel)) {
                continue;
            }

            //JSONObject location = target.getJSONObject("location");
            //StringBuffer locsb = new StringBuffer().append(location.getInteger("lat")).append(",").append(location.getInteger("lng"));

            qrTable.getItems().add(new Company(IndexUtils.getIndex() + "", tel , name, detail.getString("province")+ detail.getString("city") +detail.getString("county"), detail.getString("address"), detail.getString("category")+","+detail.getString("subcategory"),
                    keywords, MapType.map.get(mapCode), ""));

        }
    }


}
